package com.frank.oj.service.admin.problem.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.frank.model.entity.problem.Tag;
import com.frank.model.entity.problem.TagClassification;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.service.admin.problem.AdminTagService;
import com.frank.oj.service.entity.common.TagEntityService;
import com.frank.oj.service.problem.TagClassificationService;
import com.frank.oj.shiro.AccountProfile;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author frank
 * @Date 2024/4/2
 */
@Service
@Slf4j(topic = "hcoj")
public class AdminTagServiceImpl implements AdminTagService {
    @Resource
    private TagEntityService tagEntityService;
    @Resource
    private TagClassificationService tagClassificationService;

    @Override
    public Tag addTag(Tag tag) throws StatusFailException {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(tag.getGid() != null, Tag::getGid, tag.getGid())
                .eq(Tag::getName, tag.getName())
                .eq(Tag::getOj, tag.getOj());

        if (tagEntityService.count(queryWrapper) > 0) {
            throw new StatusFailException("该标签已存在");
        }

        boolean save = tagEntityService.save(tag);
        if (!save) {
            throw new StatusFailException("添加标签失败");
        }
        return tag;
    }

    @Override
    public void deleteTag(Long tid) throws StatusFailException {

        boolean removed = tagEntityService.removeById(tid);
        if (!removed) {
            throw new StatusFailException("删除标签失败");
        }
        AccountProfile userRole = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        log.info("[{}],[{}],tid:[{}],operatorUid:[{}],operatorUsername:[{}]",
                "Admin_Tag", "Delete", tid, userRole.getUid(), userRole.getUsername());
    }

    @Override
    public void updateTag(Tag tag) throws StatusFailException {
        boolean updated = tagEntityService.updateById(tag);
        if (!updated) {
            throw new StatusFailException("更新标签失败");
        }
    }

    @Override
    public List<TagClassification> getTagClassification(String oj) {

        oj = oj.toUpperCase();
        if (oj.equals("ALL")) {
            return tagClassificationService.list();
        } else {
            LambdaQueryWrapper<TagClassification> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(TagClassification::getOj, oj).orderByDesc(TagClassification::getRank);
            return tagClassificationService.list(queryWrapper);
        }
    }

    @Override
    public TagClassification addTagClassification(TagClassification tagClassification) throws StatusFailException {
        LambdaQueryWrapper<TagClassification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TagClassification::getName, tagClassification.getName())
                .eq(TagClassification::getOj, tagClassification.getOj());
        if (tagClassificationService.count(queryWrapper) > 0) {
            throw new StatusFailException("该标签分类已存在");
        }

        boolean save = tagClassificationService.save(tagClassification);
        if (!save) {
            throw new StatusFailException("添加标签分类失败");
        }
        return tagClassification;
    }

    @Override
    public void updateTagClassification(TagClassification tagClassification) throws StatusFailException {
        boolean updated = tagClassificationService.updateById(tagClassification);
        if (!updated) {
            throw new StatusFailException("更新标签分类失败");
        }
    }

    @Override
    public void deleteTagClassification(Long tcid) throws StatusFailException {
        boolean removed = tagClassificationService.removeById(tcid);
        if (!removed) {
            throw new StatusFailException("删除标签分类失败");
        }
    }


}
